package com.wjq.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自定义元数据处理对象
 * @ProjectName: reggie_take_out
 * @Package: com.wjq.reggie.common
 * @ClassName: MyMetaObjecthandler
 * @Author: 10980
 * @Description: ${description}
 * @Date: 2022/8/20 15:47
 * @Version: 1.0
 * 执行操作时才执行，要对字段填充什么进行具体处理。
 */
@Component//让spring框架管理它
@Slf4j//日志
public class MyMetaObjecthandler implements MetaObjectHandler {
    /**
     * 插入操作字段填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]");
        log.info(metaObject.toString());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

    /**
     * 更新操作字段填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]");
        log.info(metaObject.toString());
        //输出当前线程id
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}
